#include <stdio.h>
#include <stdlib.h>

FILE *file_open(const char *path, const char *mode);
int file_close(FILE **fp);

int main(int argc, char const *argv[])
{
    FILE * fp_1 = file_open("file1.txt" , "w+");
    FILE * fp_2 = file_open("file2.txt" , "r+");
    FILE * fp_3 = file_open("file3.txt" , "r+");

    // file2 清空写入到 file1
    char ch = 0;
    for (size_t i = 0; EOF != (ch = fgetc(fp_2)); i++)
    {
        fputc( ch  , fp_1 );
    }
    // 关闭文件
    file_close(&fp_1);
    // 重新打开file1
    fp_1 = file_open("file1.txt" , "a+");

    // 开始追加写
    fseek( fp_1 , 0 , SEEK_END);
    for (size_t i = 0; EOF != (ch = fgetc(fp_3)); i++)
    {
        fputc( ch  , fp_1 );
    }


    file_close(&fp_1);
    file_close(&fp_2);
    file_close(&fp_3);
    return 0;
}



FILE *file_open(const char *path, const char *mode)
{
    FILE *fp = fopen(path, mode);
    if (NULL == fp)
    {
        printf("文件创建失败\n");
        exit(-1);    // 退出整个程序
    }
    return fp;
}

int file_close(FILE **fp)
{
    fclose(*fp);
    *fp = NULL;
}
